﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace workshop3
{
    public partial class Formcourseview : Form
    {
        string[] taskid = new string[20];
        int mark = -1;
        int num = 0;
        bool editmode = false;
        string[] itemid = new string[20];
        DataTable dt4 = new DataTable();
        DataTable dt = new DataTable();
        DataColumn dc1 = new DataColumn("Year");
        DataColumn dc2 = new DataColumn("Semeter");
        DataColumn dc6 = new DataColumn("Taskname");
        DataColumn dc7 = new DataColumn("Taskid");

        DataTable dt2 = new DataTable();
        DataColumn dc4 = new DataColumn("itemid");
        DataColumn dc = new DataColumn("Itemname");
        DataColumn dc3 = new DataColumn("percentage");
        //bool change = false;
        string courseid;
        public Formcourseview()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists("course.xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("course.xml");
                string path = "//courses/course";
                XmlNodeList nodelist = doc.SelectNodes(path);
                dt.Columns.Add(dc1);
                dt.Columns.Add(dc2);
                dt4.Columns.Add(dc7);
                dt4.Columns.Add(dc6);
                dt2.Columns.Add(dc4);
                dt2.Columns.Add(dc);
                dt2.Columns.Add(dc3);
                //dt1.Columns.Add(dc3);
                List<string> yearlist = new List<string>();
                List<string> semelist = new List<string>();
               //load all the data from the xml into the datatables and arraylist
                if (nodelist != null)
                {
                    for(int i = 0; i < nodelist.Count; i++)
                    {
                        if (yearlist.Count == 0&&semelist.Count==0)
                        {
                            yearlist.Add(nodelist[i].ChildNodes[1].InnerText);
                            semelist.Add(nodelist[i].ChildNodes[2].InnerText);
                        }
                        else
                        {
                            if (yearlist.Contains(nodelist[i].ChildNodes[1].InnerText)) 
                            {
                                //continue;
                            }
                            else
                            {
                                yearlist.Add(nodelist[i].ChildNodes[1].InnerText);
                            }
                            if (semelist.Contains(nodelist[i].ChildNodes[2].InnerText))
                            {
                                //continue;
                            }
                            else
                            {
                                semelist.Add(nodelist[i].ChildNodes[2].InnerText);
                            }
                        }
                        
                    }
                }
                //before the datasource has been binded, the event should be disable, because during the data loading into combobox, the value wil be changed and activate the value change event
                comboBox_Year.SelectedValueChanged -=comboBox_Year_SelectedValueChanged ;
                comboBox_Semester.SelectedValueChanged -= comboBox_Semester_SelectedValueChanged;

                comboBox_Year.DataSource = yearlist;
                comboBox_Semester.DataSource = semelist;
                
                //after the data binding finished, enable the event
                comboBox_Year.SelectedValueChanged += comboBox_Year_SelectedValueChanged;
                comboBox_Semester.SelectedValueChanged += comboBox_Semester_SelectedValueChanged;
                
                
            }
        }

        private void label_Course_Click(object sender, EventArgs e)
        {

        }

        private void btn_Create_Click(object sender, EventArgs e)
        {
            Formcoursecreate fcc = new Formcoursecreate();
            this.Hide();
            fcc.ShowDialog();
            fcc.Owner = this;
            this.Show();

        }

        private void comboBox_Year_SelectedValueChanged(object sender, EventArgs e)
        {
            ChangeCourse( comboBox_Year.SelectedValue.ToString(), comboBox_Semester.SelectedValue.ToString());



        }

        private void ChangeCourse(string year,string  semester)//this function is to dynmaic load the course information according to the selected year and semester
        {
            DataTable dt0 = new DataTable();
            DataColumn dc3 = new DataColumn("coursename");
            DataColumn dc4 = new DataColumn("courseid");
            dt0.Columns.Add(dc4);
            dt0.Columns.Add(dc3);
            if (File.Exists("course.xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("course.xml");
                string path = "//courses/course";
                XmlNodeList nodelist = doc.SelectNodes(path);
                if (nodelist != null)
                {
                    for (int i = 0; i < nodelist.Count; i++)
                    {
                        if(nodelist[i].ChildNodes[1].InnerText==year&&nodelist[i].ChildNodes[2].InnerText==semester)
                        {
                            dt0.Rows.Add(nodelist[i].Attributes[0].Value, nodelist[i].ChildNodes[0].InnerText);
                        }
                        
                    }
                }
                dgvcourse.DataSource = dt0;//after loading the data, the new datasource need to be binded again
                //return dt0;

            }
            //return null;
        }

        private void comboBox_Semester_SelectedValueChanged(object sender, EventArgs e)
        {
            ChangeCourse(comboBox_Year.SelectedValue.ToString(), comboBox_Semester.SelectedValue.ToString());
            //dgvcourse.DataSource = dt1;
        }

        private void comboBox_Year_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ChangeCourse(dt1, comboBox_Year.SelectedValue.ToString(), comboBox_Semester.SelectedValue.ToString());
        }

        private void dgvcourse_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //textBox_CourseName.Text = string.Empty;
            dt2.Clear();
            dt4.Clear();
            
            
            
            textBox_CourseName.Text = dgvcourse.Rows[e.RowIndex].Cells[1].Value.ToString();
            courseid = dgvcourse.Rows[e.RowIndex].Cells[0].Value.ToString();
            XmlDocument xml = new XmlDocument();
            xml.Load("course.xml");
            string path = "//courses/course";
            XmlNodeList nodeList = xml.SelectNodes(path);
            for(int i = 0; i < nodeList.Count; i++)
            {
                if (nodeList[i].Attributes[0].Value == courseid)
                {
                    XmlNode ams = nodeList[i].ChildNodes[3];
                    XmlNodeList amsitem = ams.ChildNodes;
                    foreach(XmlNode item in amsitem)
                    {
                        dt2.Rows.Add(item.Attributes[0].Value,item.Attributes[1].Value, item.InnerText);
                    }
                    XmlNodeList children = nodeList[i].SelectNodes("task");
                    if (children == null)
                    {
                        MessageBox.Show("error");
                    }
                    foreach(XmlNode xn in children)
                    {
                        dt4.Rows.Add(xn.Attributes[0].Value,xn.InnerText);
                    }
                    mark = i;
                    break;
                }
            }
            dgvtask.DataSource = dt4;

        }

        private void dgvtask_MouseDown(object sender, MouseEventArgs e)
        {
           
        }

        private void dgvtask_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgvtask_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)//this function is to do the mouse right click to do the delete 
        {
            if (editmode)
            {
                if (e.Button == MouseButtons.Right)
                {

                    taskid[num] = dgvtask.Rows[e.RowIndex].Cells[0].Value.ToString();
                    dt4.Rows.Remove(dt4.Rows[e.RowIndex]);
                    num++;
                }
            }
        }

        private void btnconfirm_Click(object sender, EventArgs e)
        {
            int k = 0;
            int j = 0;
            XmlDocument xml = new XmlDocument();
            xml.Load("course.xml");
            string path = "//courses/course";
            XmlNodeList nodeList = xml.SelectNodes(path);
                XmlNodeList children = nodeList[mark].SelectNodes("task");
            XmlNode ams = nodeList[mark].ChildNodes[3];
            XmlNodeList amsitem = ams.ChildNodes;
                if (children == null)
                {
                    MessageBox.Show("error");
                }
            while (true)//first need to delete the existing data in the xml
            {
                bool removed = false;
                foreach (XmlNode xn in children)
                {
                    if (taskid != null && k <= taskid.Length)
                    {
                        if (xn.Attributes[0].Value == taskid[k])
                        {
                            nodeList[mark].RemoveChild(xn);
                            removed = true;
                        }
                       
                    }
                    if (j < dt4.Rows.Count)
                    {
                        if (dt4.Rows[j][0].ToString() == xn.Attributes[0].Value.ToString())
                        {
                            xn.InnerText = dt4.Rows[j][1].ToString();
                            removed = true;

                        }
                    }
                   
                    //dt4.Rows.Add(xn.Attributes[0].Value, xn.InnerText);
                }
                k++;
                j++;
                if (!removed)
                {
                    break;
                }
            }
            k = 0;
            j = 0;
            while (true)
            {
                bool edit = false;
                foreach(XmlNode item in amsitem)
                {
                    if (itemid != null && k < itemid.Length)
                    {
                        if (item.Attributes[0].Value == itemid[k])
                        {
                            ams.RemoveChild(item);
                            edit = true;
                        }
                    }
                    if (j < dt2.Rows.Count)//then write the new data into the xml
                    {
                        if (dt2.Rows[j][0].ToString()== item.Attributes[0].Value.ToString())
                        {
                            item.InnerText = dt2.Rows[j][2].ToString();
                            item.Attributes[1].Value = dt2.Rows[j][1].ToString();
                            edit = true;
                        }
                    }
                }
                k++;
                j++;
                if (!edit)
                {
                    break;
                }
                
            }
            xml.Save("course.xml");
            //change all the component state into non-edit mode
            editmode = false;
            btnconfirm.Visible = false;
            btnconfirm.Enabled = false;
            dgvtask.ReadOnly = true;
            comboBox_Year.Enabled = true;
            comboBox_Semester.Enabled = true;
            dgvcourse.Enabled = true;
            MessageBox.Show("Edit successfully");
            //dgvtask.DataSource = null;
        }

        private void btn_AMS_Click(object sender, EventArgs e)
        {
            Formviewams viewams = new Formviewams(dt2,editmode,courseid);
            viewams.Owner = this;
            viewams.ShowDialog();
            itemid = viewams.itemid;
            dt2 = viewams.dtviewams;
        }

        private void btn_Edit_Click(object sender, EventArgs e)
        {
            //change all the component state into edit mode
            editmode = true;
            btnconfirm.Visible = true;
            btnconfirm.Enabled = true;
            dgvtask.ReadOnly = false;
            dgvcourse.Enabled = false;
            comboBox_Semester.Enabled = false;
            comboBox_Year.Enabled = false;
            //dgvtask.Enabled = true;
        }

        private void btndelete_Click(object sender, EventArgs e)//delete the selected course
        {
            bool delete=ClassCreateXML.deletexml("course.xml", "//course", courseid);
            if (delete)
            {
                MessageBox.Show("Delete success!");
            }
            dt.Clear();
            dt2.Clear();
            dt4.Clear();
            this.Form1_Load(null, null);
            //this.InitializeComponent();

        }

        private void btn_back_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void btn_user_Click(object sender, EventArgs e)
        {
            Formmanagement back = new Formmanagement();
            back.Show();
            this.Hide();
        }
    }
}
